# Maintainer: Antonio Rojas <arojas@archlinux.org>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - add sse=0 to objcryst make line

pkgname=genxrdpattern
pkgver=1.1
pkgrel=4
pkgdesc='Uses ObjCryst++ to generate a theoretical XRD pattern'
arch=(x86_64)
url='https://github.com/psavery/genXrdPattern'
license=(GPL-2.0-or-later)
depends=(gcc-libs
         glibc)
makedepends=(cmake
             git)
source=(git+https://github.com/psavery/genXrdPattern#tag=$pkgver-linux
        https://github.com/psavery/genXrdPattern/releases/download/$pkgver-linux/objcryst.tgz)
sha256sums=('02de8acb488a9a51e09882bf67dbbea89406a98c92bebdc1de66cf2aad74cc91'
            '5523eae9937388260b3e491a1a11a443ab00a0961318ec92eee08f9366a0953f')

prepare() {
  sed -e 's|/user/psavery/src/objcryst/Fox/src|.|g' -i objcryst/Fox/src/Fox.dep
  sed -e 's|-march=native||' -i objcryst/ObjCryst/*.mak # Don't use -march=native
  sed -e 's|set(CMAKE_EXE_LINKER_FLAGS \"-static\")|set(CMAKE_EXE_LINKER_FLAGS "$ENV{LDFLAGS}")|' \
    -i genXrdPattern/CMakeLists.txt # Honor system LDFLAGS
  sed -e 's/indx > 0/*indx > 0/' -i objcryst/newmat/newmat6.cpp
  sed -e "s|CPPFLAGS = -w -ffast-math.*|CPPFLAGS = ${CXXFLAGS}|" -i objcryst/ObjCryst/*.mak # Honor system CFLAGS
  sed -e "s|CXXFLAGS = -O2 -Wall|CXXFLAGS = ${CXXFLAGS}|" -i objcryst/{cctbx/gnu.mak,newmat/nm_gnu.mak}
}

build() {
  cd objcryst/Fox
  make Fox-nogui shared=0 sse=0 -j1

  cd "$srcdir"
  cmake -B build -S genXrdPattern \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DObjCryst_ROOT="$srcdir"/objcryst
  cmake --build build
}

package() {
  cd build
  install -Dm755 genXrdPattern -t "$pkgdir"/usr/bin
}
